package com.kkd.kkdapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.fragment.MyDiscountCouponHasBeenUsedFragment;
import com.kkd.kkdapp.fragment.MyDiscountCouponHaveExpiredFragment;
import com.kkd.kkdapp.fragment.MyDiscountCouponUnusedFragment;
import com.kkd.kkdapp.utils.SystemUtils;

/**
 * 我的优惠券
 * 
 * @author mty
 * 
 */
public class MyDiscountCouponActivity extends BaseActivity implements
		OnClickListener {

	private RelativeLayout deal_top_img; // 返回
	private RelativeLayout regulations; // 使用规则
	private PopupWindow pop;
	// 已使用
	private MyDiscountCouponHasBeenUsedFragment hasBeenUsedFragment = new MyDiscountCouponHasBeenUsedFragment();
	private FrameLayout hasbeenused_fragment;
	// 已过期
	private MyDiscountCouponHaveExpiredFragment haveExpiredFragment = new MyDiscountCouponHaveExpiredFragment();
	FrameLayout haveexpired_fragment;
	// 未使用
	private MyDiscountCouponUnusedFragment unusedFrament = new MyDiscountCouponUnusedFragment();
	private FrameLayout unused_fragment;
	private RelativeLayout not_used_button; // 未使用
	private TextView not_used_tv;
	private View not_used_view;
	private RelativeLayout already_used_button; // 已使用
	private TextView already_used_tv;
	private View already_used_view;
	private RelativeLayout expired_button; // 已过期
	private TextView expired_tv;
	private View expired_view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_discount_coupon);
		initView();
		setListener();
	}

	public void initView() {
		deal_top_img = (RelativeLayout) this.findViewById(R.id.deal_top_img);
		regulations = (RelativeLayout) this.findViewById(R.id.regulations);
		unused_fragment = (FrameLayout) this.findViewById(R.id.unused_fragment);
		hasbeenused_fragment = (FrameLayout) this
				.findViewById(R.id.hasbeenused_fragment);
		haveexpired_fragment = (FrameLayout) this
				.findViewById(R.id.haveexpired_fragment);
		not_used_button = (RelativeLayout) this
				.findViewById(R.id.not_used_button);
		not_used_tv = (TextView) this.findViewById(R.id.not_used_tv);
		not_used_view = this.findViewById(R.id.not_used_view);
		already_used_button = (RelativeLayout) this
				.findViewById(R.id.already_used_button);
		already_used_tv = (TextView) this.findViewById(R.id.already_used_tv);
		already_used_view = this.findViewById(R.id.already_used_view);
		expired_button = (RelativeLayout) this
				.findViewById(R.id.expired_button);
		expired_tv = (TextView) this.findViewById(R.id.expired_tv);
		expired_view = this.findViewById(R.id.expired_view);
		initFragmentView();
	}

	public void setListener() {
		deal_top_img.setOnClickListener(this);
		regulations.setOnClickListener(this);
		not_used_button.setOnClickListener(this);
		already_used_button.setOnClickListener(this);
		expired_button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.deal_top_img: // 返回
			finish();
			break;
		case R.id.regulations: // 使用规则
			SystemUtils.onEvent("Account_Coupon_Rules", 1);
			showPopup(v);
			break;
		case R.id.not_used_button: // 未使用
			settingUnused();
			break;
		case R.id.already_used_button: // 已使用
			settingunderway();
			break;
		case R.id.expired_button: // 已过期
			settingclassify();
			break;
		}

	}

	/**
	 * 未使用
	 */
	public void settingUnused() {
		setDefaultFragment(0, "1");
		// 未使用
		not_used_tv.setTextColor(Color.parseColor("#543c9f"));
		not_used_view.setVisibility(View.VISIBLE);
		// 已使用
		already_used_tv.setTextColor(Color.parseColor("#333333"));
		already_used_view.setVisibility(View.GONE);
		// 已过期
		expired_tv.setTextColor(Color.parseColor("#333333"));
		expired_view.setVisibility(View.GONE);
	}

	/**
	 * 已使用
	 */
	public void settingunderway() {
		setDefaultFragment(1, "4");
		// 未使用
		not_used_tv.setTextColor(Color.parseColor("#333333"));
		not_used_view.setVisibility(View.GONE);
		// 已使用
		already_used_tv.setTextColor(Color.parseColor("#543c9f"));
		already_used_view.setVisibility(View.VISIBLE);
		// 已过期
		expired_tv.setTextColor(Color.parseColor("#333333"));
		expired_view.setVisibility(View.GONE);
	}

	/**
	 * 已过期
	 */
	public void settingclassify() {
		setDefaultFragment(2, "2");
		// 未使用
		not_used_tv.setTextColor(Color.parseColor("#333333"));
		not_used_view.setVisibility(View.GONE);
		// 已使用
		already_used_tv.setTextColor(Color.parseColor("#333333"));
		already_used_view.setVisibility(View.GONE);
		// 已过期
		expired_tv.setTextColor(Color.parseColor("#543c9f"));
		expired_view.setVisibility(View.VISIBLE);
	}

	public void initFragmentView() {
		FragmentTransaction tran = getSupportFragmentManager()
				.beginTransaction();
		tran.replace(R.id.unused_fragment, unusedFrament);
		tran.replace(R.id.hasbeenused_fragment, hasBeenUsedFragment);
		tran.replace(R.id.haveexpired_fragment, haveExpiredFragment);
		tran.commit();
		settingUnused();
	}

	/**
	 * Fragment 替换
	 * 
	 * @param status
	 */
	public void setDefaultFragment(int status, String couponType) {
		unused_fragment.setVisibility(status == 0 ? View.VISIBLE : View.GONE);
		hasbeenused_fragment.setVisibility(status == 1 ? View.VISIBLE
				: View.GONE);
		haveexpired_fragment.setVisibility(status == 2 ? View.VISIBLE
				: View.GONE);
	}

	/**
	 * 使用规则
	 */
	public void showPopup(View v) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.regulations_popupwindow, null);
		pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT, false);
		pop.setOutsideTouchable(true);
		RelativeLayout close_tv = (RelativeLayout) view
				.findViewById(R.id.close_tv);
		pop.showAtLocation(v, 0, 0, 0);
		close_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				pop.dismiss();
				final WindowManager.LayoutParams attrs = getWindow()
						.getAttributes();
				attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
				getWindow().setAttributes(attrs);
				getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
			}
		});
	}
}
